package com.xiaoyi.threadpoollibrary;


import java.util.Collection;
import java.util.HashMap;
import java.util.Map;

/**
 * 线程池实例管理。
 * 
 * @author <a href="mailto:aofengblog@163.com">聂勇</a>
 */
public class ThreadPoolManager implements ILifeCycle {

    private ILifeCycle _threadPool = new ThreadPoolImpl();
    private static Object _lock = new Object();
    private boolean _initStatus = false;
    private boolean _destroyStatus = false;
    private static ThreadPoolManager _instance = new ThreadPoolManager();
    public static ThreadPoolManager getSingleton() {
        if(_instance == null) {
            _instance = new ThreadPoolManager();
        }
        return _instance;
    }

    public ThreadPool getThreadPool() {
        return (ThreadPool) _threadPool;
    }

    /**
     * key为线程池名称，value为{@link ThreadPoolInfo}实例。
     */
    protected  static     Map<String, ThreadPoolInfo> _multiThreadPoolInfo = new HashMap<String, ThreadPoolInfo>();

    /** 线程池状态收集开关 */
    public static boolean _threadPoolStateSwitch = true;
    public static int _threadPoolStateInterval = 60;   // 单位：秒

    /** 线程状态收集开关 */
    public static boolean _threadStateSwitch = true;
    public static int _threadStateInterval = 60;   // 单位：秒

    /** 线程堆栈收集开关 */
    public static boolean _threadStackSwitch = true;
    public static int _threadStackInterval = 60;   // 单位：秒


    
    // 用于单元测试和子类扩展
    protected void setThreadPool(ThreadPool threadPool) {
        this._threadPool = (ILifeCycle) threadPool;
    }
    
    @Override
    public void init() {
        synchronized (_lock) {
            if (_initStatus) {
                return;
            }
            initConfig();
            _threadPool.init();
            _initStatus = true;
        }
    }


    public ThreadPoolManager setThreadPoolStateSwitch(boolean b){
        ThreadPoolManager._threadPoolStateSwitch = b;
        return _instance;
    }
    public ThreadPoolManager setThreadPoolStateInterval(int i){
        ThreadPoolManager._threadPoolStateInterval = i;
        return _instance;
    }

    public ThreadPoolManager setThreadStateSwitch (boolean b){
        ThreadPoolManager._threadStateSwitch  = b;
        return _instance;
    }
    public ThreadPoolManager setThreadStateInterval(int i){
        ThreadPoolManager._threadStateInterval = i;
        return _instance;
    }

    public ThreadPoolManager setThreadStackSwitch(boolean b){
        ThreadPoolManager._threadStackSwitch = b;
        return _instance;
    }
    public ThreadPoolManager setThreadStackInterval(int i){
        ThreadPoolManager._threadStackInterval = i;
        return _instance;
    }

    public ThreadPoolManager builder(){
        init();
        return _instance;
    }

    public  ThreadPoolManager addDiyThreadPool(ThreadPoolInfo info){
        _multiThreadPoolInfo.put(info.getName(), info);
        return _instance;
    }

    private void initConfig() {
        ThreadPoolInfo info = new ThreadPoolInfo();
        info.setName("default");
        info.setCoreSize(10);
        info.setMaxSize(100);
        info.setThreadKeepAliveTime(15);
        info.setQueueSize(10000);
        _multiThreadPoolInfo.put(info.getName(), info);

        info = new ThreadPoolInfo();
        info.setName("other");
        info.setCoreSize(10);
        info.setMaxSize(100);
        info.setThreadKeepAliveTime(15);
        info.setQueueSize(10000);
        _multiThreadPoolInfo.put(info.getName(), info);
    }
    @Override
    public void destroy() {
        synchronized (_lock) {
            if (_destroyStatus) {
                return;
            }
            _threadPool.destroy();
            _destroyStatus = true;
        }
    }
    //干掉所有线程
    public static void destroyAllThread(){
        _threadPoolStateSwitch = false;
        _threadStateSwitch = false;
        _multiThreadPoolInfo.clear();
    }
    /**
     * 指定名称的线程池的配置是否存在。
     *
     * @return 如果指定名称的线程池的配置存在返回true，如果不存在返回false；如果传入的线程池名称为null也返回false。
     */
    public static boolean containsPool(String poolName) {
        if (null == poolName || null == _multiThreadPoolInfo || _multiThreadPoolInfo.isEmpty()) {
            return false;
        }

        return _multiThreadPoolInfo.containsKey(poolName);
    }

    /**
     * 获取指定线程池的配置信息。
     *
     * @param threadpoolName 线程池名称
     * @return 线程池配置信息（{@link ThreadPoolInfo}）
     */
    public static ThreadPoolInfo getThreadPoolConfig(String threadpoolName) {
        return _multiThreadPoolInfo.get(threadpoolName);
    }

    /**
     * 获取所有线程池的配置信息。
     *
     * @return 线程池配置信息（{@link ThreadPoolInfo}）集合
     */
    public static Collection<ThreadPoolInfo> getThreadPoolConfig() {
        return _multiThreadPoolInfo.values();
    }

    /**
     * @return 输出各个线程池状态信息的开关，true表示开，false表示关
     */
    public static boolean getThreadPoolStateSwitch() {
        return _threadPoolStateSwitch;
    }

    /**
     * @return 线程池状态信息输出的间隔时间（单位：秒）
     */
    public static int getThreadPoolStateInterval() {
        return _threadPoolStateInterval;
    }

    /**
     * @return 输出各个线程组中线程状态信息的开关，true表示开，false表示关
     */
    public static boolean getThreadStateSwitch() {
        return _threadStateSwitch;
    }

    /**
     * @return 线程状态信息输出的间隔时间（单位：秒）
     */
    public static int getThreadStateInterval() {
        return _threadStateInterval;
    }

    /**
     * @return 输出所有线程堆栈的开关，true表示开，false表示关
     */
    public static boolean getThreadStackSwitch() {
        return _threadStackSwitch;
    }

    /**
     * @return 线程堆栈信息输出的间隔时间（单位：秒）
     */
    public static int getThreadStackInterval() {
        return _threadStackInterval;
    }
}
